package repository;
import fileoperation.FileCreation;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Repository {
    private static String workTree;	//working directory
    private static String gitDir;	//jit repository path

    /**
     * Constructor
     */
    public Repository(){
        if(gitDir == ""){
            System.out.println("The repository does not exist!");
        }
    }

    /**
     * Constructor
     * @param path
     * @throws IOException
     */
    public Repository(String path) throws IOException {
        this.workTree = path;
        this.gitDir = path + File.separator + ".jit";
    }

    public static String getGitDir() {
        return gitDir;
    }

    public static String getWorkTree() {
        return workTree;
    }

    /**
     * Helper functions.
     * @return
     */
    public boolean exist(){ return new File(gitDir).exists(); }

    public boolean isFile(){ return new File(gitDir).isFile(); }

    public boolean isDirectory(){ return new File(gitDir).isDirectory(); }


    /**
     * Create the repository and files and directories inside.
     * @return boolean
     * @throws IOException
     */
    public void createRepo() throws IOException {

        File file = new File(gitDir);
        file.mkdir();
        
        FileCreation.createDirectory(workTree, ".jit"); //创建仓库目录

        FileCreation.createDirectory(gitDir, "hooks"); //存储jit命令需要用到的自定义脚本
        FileCreation.createDirectory(gitDir, "info"); //创建info目录，存储仓库信息
        FileCreation.createDirectory(gitDir, "objects"); //创建objects目录，保存hash文件
        FileCreation.createDirectory(gitDir, "branches"); //创建branches目录，基于commit创建分支信息
        FileCreation.createDirectory(gitDir, "refs"); //创建refs目录，存储分支信息
        FileCreation.createDirectory(gitDir, "refs", "heads");
        FileCreation.createDirectory(gitDir, "refs", "tags");
        FileCreation.createDirectory(gitDir, "logs"); //创建logs目录，存储各分支下的commit记录
        FileCreation.createDirectory(gitDir, "logs", "refs");
        FileCreation.createDirectory(gitDir, "logs", "refs", "heads");
        FileCreation.createDirectory(gitDir, "index");//创建暂存区

        FileCreation.createFile(gitDir, "HEAD", "ref: refs/heads/master"); //创建“HEAD”文件，存储HEAD指向的分支
        FileCreation.createFile(gitDir, "description", "Unnamed repository; edit this file 'description' to name the repository.\n"); //创建仓库描述文件以存储相关信息
        FileCreation.createFile(gitDir, "config", getDefaultConfig()); //创建配置文件
        FileCreation.createFile(gitDir, "COMMIT_EDITMSG", null); //创建文件以保存commit信息

    }

    /**
     * Getting default configuration
     * @return
     */
    public String getDefaultConfig() {
        String default_config = "[core]\n"
                + "        repositoryformatversion = 0\n"
                + "        filemode = gitDir\n"
                + "        bare = false\n"
                + "        logallrefupdates = gitDir\n"
                + "        ignorecase = gitDir\n"
                + "        precomposeunicode = gitDir";
        return default_config;
    }
}